﻿Imports System.IO.IsolatedStorage
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Graphics
Imports Runa.Common.Helper
Imports System.IO

Namespace Global.Runa.Resources.ContentManagers

    Public Class IsolatedContentManager
        Inherits BasicContentManager

        Public Property IsolatedStorageRootDirectory() As String

#If WINDOWS_PHONE Or NETFX_CORE Then
        Private m_fileStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
#ElseIf WINDOWS Then
        Private m_fileStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForAssembly()
#End If

        Public Sub New(serviceProvider As System.IServiceProvider, Optional ByVal fs As IsolatedStorageFile = Nothing)
            MyBase.New(serviceProvider)
            Me.m_fileStorage = If(fs, Me.m_fileStorage)
        End Sub

        Public Sub New(serviceProvider As System.IServiceProvider, rootDirectory As String, Optional ByVal fs As IsolatedStorageFile = Nothing)
            MyBase.New(serviceProvider, rootDirectory)
            Me.IsolatedStorageRootDirectory = rootDirectory
            Me.m_fileStorage = If(fs, Me.m_fileStorage)
        End Sub

        Public Overrides Function Exists(ByVal assetName As String) As Boolean
            Return Me.m_fileStorage.FileExists(Path.Combine(Me.IsolatedStorageRootDirectory, assetName))
        End Function

        Protected Overrides Function OpenStream(assetName As String) As System.IO.Stream
            Return Me.m_fileStorage.OpenFile(Path.Combine(Me.IsolatedStorageRootDirectory, assetName), System.IO.FileMode.Open)
        End Function

    End Class

End Namespace